package com.ssyc.gsk_teacher.activity;

import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.RelativeLayout;

import com.ssyc.common.base.BaseActivity;
import com.ssyc.common.base.BaseApplication;
import com.ssyc.common.http.HttpAdress;
import com.ssyc.common.http.HttpUtils;
import com.ssyc.common.manager.AccountUtils;
import com.ssyc.common.utils.GsonUtil;
import com.ssyc.common.utils.UiUtils;
import com.ssyc.common.view.MyGridView;
import com.ssyc.gsk_teacher.R;
import com.ssyc.gsk_teacher.adapter.WorkQuerySutdentGvAdapter;
import com.ssyc.gsk_teacher.bean.StudentQueryInfo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.Call;

/**
 * 教师端--查看练习布置详情界面
 * Created by Administrator on 2018/5/8 0008.
 */

public class TeacherQueryHomeWorkActivity extends BaseActivity implements View.OnClickListener {
    //返回键
    private RelativeLayout rl_back_top;


    //展示所有学生的GridView
    private MyGridView mgv;

    //展示学生列表的适配器
    private WorkQuerySutdentGvAdapter workSutdentGvAdapter;

    //展示学生信息的数据集合
    private List<StudentQueryInfo.DataBean> oldDatas;

    //班级id
    private String classNum;

    //练习id
    private String workId;


    @Override
    protected boolean isNeedShowTitle() {
        return false;
    }

    @Override
    protected String setTitleText() {
        return null;
    }

    @Override
    protected int getContentLayout() {
        return R.layout.activity_teacher_query_homework;
    }

    @Override
    protected void init() {
        //初始化视图
        initView();

        showContent();

        initIntent();

        http();

    }


    /**
     * 接收传值
     */
    private void initIntent() {

        classNum = getIntent().getStringExtra("classNum");
        workId = getIntent().getStringExtra("workId");


    }


    /**
     * 请求服务器
     */
    private void http() {

        Map<String, String> map = new HashMap<>();
        map.put("type", "54");
//        map.put("acc", "13006314760");
        map.put("acc", AccountUtils.getAccount(this));
        map.put("role", "2");
        map.put("classNum", classNum);
        map.put("workId", workId);
        map.put("platform", "8");
//        map.put("apptoken", "WHBHWJDSAKDAKSDNSAJDAK");
      map.put("apptoken", AccountUtils.getToken(this));
        HttpUtils.post(HttpAdress.HOMEWORKLIST, map, this, new HttpUtils.CallBack() {
            @Override
            public void onError(Call call, Exception e, int id) {
                UiUtils.Toast(BaseApplication.ERROR, false);
            }

            @Override
            public void onResponse(String response, int id) {

                if (!TextUtils.isEmpty(response)) {
                    StudentQueryInfo info = null;
                    try {
                        info = GsonUtil.jsonToBean(response, StudentQueryInfo.class);
                    } catch (Exception e) {
                        Log.i("test", "数据解析异常");
                    }

                    Log.i("test", "布置情况JSON"+response);
                    if (null != info) {
                        if ("200".equals(info.getState())) {

                            List<StudentQueryInfo.DataBean> newDatas = info.getData();

                            oldDatas.clear();
                            oldDatas.addAll(newDatas);

                            initData();

                        } else {
                            Log.i("test", "错误,状态码是:" + info.getState());
                        }
                    } else {
                        Log.i("test", "返回数据为空");
                    }
                } else {
                    Log.i("test", "返回数据为空");
                }
            }
        });
    }


    /**
     * 初始化数据
     */
    private void initData() {

        workSutdentGvAdapter = new WorkQuerySutdentGvAdapter(this, oldDatas,
                R.layout.teacher_item_gb_students);
        mgv.setAdapter(workSutdentGvAdapter);
    }


    /**
     * 初始化视图的方法
     */
    public void initView() {
        rl_back_top = findViewById(R.id.rl_back_top);
        rl_back_top.setOnClickListener(this);
        mgv = findViewById(R.id.mgv);

        oldDatas = new ArrayList<>();
    }


    /**
     * 页面控件点击事件
     *
     * @param v
     */
    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.rl_back_top) {
            //返回
            finish();
        }
    }
}
